package com.li.spring.aop.xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class LoggerAspect {


    public void beforeAdviceMethod(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("LoggerAspect, 方法: " + signature.getName() + "，参数: " + Arrays.toString(args));
    }

    public void afterAdviceMethod(){
        System.out.println("LoggerAspect, 后置通知");
    }

    public void afterReturningAdviceMethod(JoinPoint joinPoint, Object result){
        System.out.println("LoggerAspect, 返回通知");
    }

    public void afterThrowingAdviceMethod(JoinPoint joinPoint, Throwable e){
        Signature signature = joinPoint.getSignature();
        System.out.println("LoggerAspect, 方法:" + signature.getName() + "，异常:" + e);
    }

    public Object aroundAdviceMethod(ProceedingJoinPoint proceedingJoinPoint){
        Object proceed = null;
        try {
            System.out.println("环绕通知，前置通知的位置");
            //表示目标方法的执行
            proceed = proceedingJoinPoint.proceed();
            System.out.println("环绕通知， 返回通知的位置");
        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println("环绕通知， 异常通知的位置");
        } finally {
            System.out.println("环绕通知， 后置通知的位置");
        }
        return proceed;
    }
}
